import 'dart:math';

/// Created by XieXin on 2020/12/22.
/// 随机数工具类
class RandomUtils {
  ///根据min和max随机生成一个范围在[min,max]的随机数，包括min和max
  static int getRandom(int min, int max) {
    Random random = Random();
    return random.nextInt(max - min + 1) + min;
  }

  ///根据min和max随机生成count个不重复的随机数组
  static List<int> getRandoms(int min, int max, int count) {
    List<int> randoms = List.filled(count, 0);
    List<int> listRandom = [];

    if (count > (max - min + 1)) return [];
    // 将所有的可能出现的数字放进候选list
    for (int i = min; i <= max; i++) {
      listRandom.add(i);
    }
    // 从候选list中取出放入数组，已经被选中的就从这个list中移除
    for (int i = 0; i < count; i++) {
      int index = getRandom(0, listRandom.length - 1);
      randoms[i] = listRandom[index];
      listRandom.removeAt(index);
    }
    return randoms;
  }

  ///根据字符串列表随机生成不重复的随机字母数组
  static List<String> getRandomStrings(List<String> list) {
    List<String> randoms = List.filled(list.length, "");
    List<String> listRandom = list.toList();
    // 从候选list中取出放入数组，已经被选中的就从这个list中移除
    for (int i = 0; i < list.length; i++) {
      int index = getRandom(0, listRandom.length - 1);
      randoms[i] = listRandom[index];
      listRandom.removeAt(index);
    }
    return randoms;
  }

  ///随机生成26个不重复的随机字母数组
  static List<String> getLetters() {
    List<String> randoms = List.filled(26, "");
    List<String> listRandom = letters.toList();
    // 从候选list中取出放入数组，已经被选中的就从这个list中移除
    for (int i = 0; i < 26; i++) {
      int index = getRandom(0, listRandom.length - 1);
      randoms[i] = listRandom[index];
      listRandom.removeAt(index);
    }
    return randoms;
  }

  ///26个大写字母
  static List<String> letters = [
    "A",
    "B",
    "C",
    "D",
    "E",
    "F",
    "G",
    "H",
    "I",
    "J",
    "K",
    "L",
    "M",
    "N",
    "O",
    "P",
    "Q",
    "R",
    "S",
    "T",
    "U",
    "V",
    "W",
    "X",
    "Y",
    "Z",
  ];
}
